package com.black.platform.fdfs.web;

import com.black.common.auth.UserContextHolder;
import com.black.common.upload.config.MyOosConfig;
import com.black.common.web.ApiResponse;
import com.black.common.web.ApiResponseBuilder;
import com.black.platform.fdfs.service.FastDfsService;
import com.black.platform.user.service.bz.ShareUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author linwenfeng
 */
@RestController
@RequestMapping("attachment")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FastDfsController {

    private final FastDfsService fastDfsService;

    private final ShareUserService shareUserService;

    @PostMapping("uploadIcon")
    public ApiResponse<String> uploadIcon(MultipartFile file) throws Exception {
        String path = fastDfsService.upload(file, FilenameUtils.getExtension(file.getOriginalFilename()));
        shareUserService.updateIcon(UserContextHolder.getUserId(),path);
        return ApiResponseBuilder.ok(path);
    }

    @PostMapping("upload")
    public ApiResponse<String> upload(MultipartFile file) throws Exception {
        String path = fastDfsService.upload(file, FilenameUtils.getExtension(file.getOriginalFilename()));
        return ApiResponseBuilder.ok(MyOosConfig.domain+path);
    }
}
